package com.zyh.find;

import java.util.Arrays;

public class FindDemo1 {
    public static void main(String[] args) {
        /*
        二分查找{5,12,90,18,77,76,45,28,59,72},    找72的索引
         */
        int n = 72;
        int[] bubbleSort = {5,12,90,18,77,76,45,28,59,72};
        int[] sort = bubbleSort(bubbleSort);
//        for (int i = 0; i < bubbleSort.length; i++) {
//            System.out.print(bubbleSort[i]+"\t");
//        }
        System.out.println();
        find(n, sort);
    }

    private static void find(int n, int[] bubbleSort) {
        int min = 0, max = bubbleSort.length-1, mid;
        while (min<=max) {
            mid = (min + max) / 2;
            if (n > bubbleSort[mid]) {//说明n在mid右边
                min = mid + 1;
            } else if (n < bubbleSort[mid]) {//说明n在mid左边
                max = mid - 1;
            } else {
                System.out.println(n + "在数组中的索引是" + mid);
                return;
            }
        }
        System.out.println(n +"在数组中不存在！");
    }

    private static int[] bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j]> arr[j+1]){
                    int temp= arr[j];
                    arr[j]= arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        return arr;
    }
}
